package sustav.gui;


import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import sustav.PostavkeSustava;



public class KomponentaPostavke extends JComponent {
	
	JPanel panel1,panel2,panel3,panel4,panel5,panel6;
	
	public KomponentaPostavke(){
	
		panel1=napraviNoviPanel("P(A)","0.1");
		panel2=napraviNoviPanel("P(B)","0.2");
		panel3=napraviNoviPanel("P(C)","0.3");
		panel4=napraviNoviPanel("P(D)","0.4");
		panel5=napraviNoviPanel("duljina","1000");
		panel6=napraviNoviPanel("P(error)","0.2");
		
		
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		
		this.add(panel1);
		this.add(panel2);
		this.add(panel3);
		this.add(panel4);
		this.add(panel5);
		this.add(panel6);
		
		this.setBorder(new TitledBorder("Postavke sustava"));
		this.validate();
	}
	
	private JPanel napraviNoviPanel(String s,String s1){
		
		JPanel panel=new JPanel();
		JTextField text=new JTextField(s1);
		JLabel label1 =new JLabel(s);
		
		
		panel.setLayout(new BorderLayout());
		
		text.setPreferredSize(new Dimension(50,25));
		text.setEditable(false);
		panel.add(text,BorderLayout.EAST);
		panel.add(label1,BorderLayout.WEST);
		
		panel.validate();
		
		
		return panel;
		
	}
	
	public PostavkeSustava getPostavkeSustava(){
		PostavkeSustava postavke=new PostavkeSustava();
		
	 float[] vjerojatnostSimbola=new float[4];
	 float vjerojatnostGreske;
	 char[] simboli={'a','b','c','d'};
	 int duljina;
	 
	  vjerojatnostSimbola[0]=Float.valueOf((((JTextField) (panel1.getComponent(0))).getText()));
	  vjerojatnostSimbola[1]=Float.valueOf((((JTextField) (panel2.getComponent(0))).getText()));
	  vjerojatnostSimbola[2]=Float.valueOf((((JTextField) (panel3.getComponent(0))).getText()));
	  vjerojatnostSimbola[3]=Float.valueOf((((JTextField) (panel4.getComponent(0))).getText()));
	  duljina=Integer.valueOf((((JTextField) (panel5.getComponent(0))).getText()));
	  vjerojatnostGreske=Float.valueOf((((JTextField) (panel5.getComponent(0))).getText()));
	  
	  postavke.setSimboli(simboli);
	  postavke.setVjerojatnostSimbola(vjerojatnostSimbola);
	  postavke.setBrojZnakovaPoruke(duljina);
	  postavke.setVjerojatnostPogresnogPrjenosa(vjerojatnostGreske);
	  return postavke;
		
	}
	
}
